package org.abettor.nookinfo.photo;

import java.util.ArrayList;
import java.util.List;

import org.abettor.nookinfo.R;
import org.abettor.nookinfo.modal.Pair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 选择图片库
 * @author shawn
 *
 */
public class PhotoSelect extends Activity
{
	@Override
    public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_select);
        GridView gridView = (GridView) findViewById(R.id.photoSelectGridView);
        SelectionAdapter adapter = new SelectionAdapter();
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new OnItemClickListener());
	}

	/**
	 * 显示按钮的适配类
	 * @author shawn
	 *
	 */
	private class SelectionAdapter extends BaseAdapter
	{
		private List<Pair<Integer, Integer>> itemList = new ArrayList<Pair<Integer, Integer>>();

		public SelectionAdapter()
		{
			super();

			Pair<Integer, Integer> pair = new Pair<Integer, Integer>();
			pair.first = R.drawable.photo_select_internet;
			pair.second = R.string.photo_select_btn_internet;
			itemList.add(pair);

			pair = new Pair<Integer, Integer>();
			pair.first = R.drawable.photo_select_local;
			pair.second = R.string.photo_select_btn_local;
			itemList.add(pair);
		}

		@Override
		public int getCount()
		{
			return itemList.size();
		}

		@Override
		public Object getItem(int position)
		{
			return itemList.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			return itemList.get(position).first;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			Pair<Integer, Integer> item = itemList.get(position);
			LinearLayout layout = new LinearLayout(PhotoSelect.this);
			layout.setOrientation(LinearLayout.VERTICAL);

			LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			params1.gravity = 0x01;
			ImageView imageView = new ImageView(PhotoSelect.this);
			imageView.setBackgroundResource(item.first);
			imageView.setLayoutParams(params1);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);

			LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			params2.gravity = 0x01;
			TextView textView = new TextView(PhotoSelect.this);
			textView.setText(item.second);
			textView.setTextColor(0xFF000000);
			textView.setLayoutParams(params2);

			layout.addView(imageView);
			layout.addView(textView);
			return layout;
		}
	}

	/**
	 * 点击事件处理类
	 * @author shawn
	 *
	 */
	public class OnItemClickListener implements AdapterView.OnItemClickListener
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id)
		{
			Intent intent = new Intent();
			if(position == 0)
			{
				intent.setClass(PhotoSelect.this, PhotoCategory.class);
			}
			else
			{
				//	本地图片分类列表
				intent.setClass(PhotoSelect.this, PhotoLocalCategory.class);
			}

			startActivity(intent);
		}
	}
}
